﻿using B.MES.SLY.Service;
using B.MES.SLY.Service.Input.Record;
using B.MES.SLY.Service.OutPut;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Standard;
using Yitter.IdGenerator;

namespace B.MES.SLY.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RecordController : ControllerBase
    {
        private readonly IMesService mesService;

        public RecordController(IMesService mesService)
        {
            this.mesService = mesService;
        }
        private static int serialNumber = 1; // 静态变量，用于记录流水号，初始为1

        /// <summary>
        /// 编码（流水号）
        /// </summary>
        /// <param name="codeType"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public string GetCode()
        {
            var result = new ApiResult<string>();
            //获取表中的数量，在该数量上加1
            var count = mesService.GetSalesCount().ResultData.Count;
            var serialNumber = count + 1;
            string serialPart = serialNumber.ToString("D7");
            string code = "JNZG-DD" + DateTime.Now.ToString("yyyyMMddHH") + serialPart;
            serialNumber++;
            return code;

        }
        /// <summary>
        /// 联系记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiPaging<GetRelstionOutPut> GetRelstion(string? ID)
        {
           return mesService.GetRelstion(ID);
        }
        /// <summary>
        /// 收款记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiPaging<GetCollectionOutPut> GetCollection(string? ID)
        {
           return mesService.GetCollection(ID);
        }
        /// <summary>
        /// 销售记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiPaging<GetSalesOutPut> GetSales(string? ID)
        {
           return mesService.GetSales(ID);
        }
        /// <summary>
        /// 发票记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiPaging<GetInvoiceOutPut> GetInvoice(string? ID)
        {
            return mesService.GetInvoice(ID);
        }
        /// <summary>
        /// 出库记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiPaging<GetDeliveryOutPut> GetDelivery(string? ID)
        {
            return mesService.GetDelivery(ID);
        }
        /// <summary>
        /// 添加联系记录
        /// </summary>
        /// <param name="addRelationInput"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> AddRelation(AddRelationInput addRelationInput)
        {
            return mesService.AddRelation(addRelationInput);
        }
        /// <summary>
        /// 添加销售记录
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> AddSales(AddSalesInput Input)
        {
            Input.Sale_Code = GetCode();
           return mesService.AddSales(Input);
        }

        /// <summary>
        /// 发票列表添加
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public ApiResult<int> AddInvoice(AddInvoice Input)
        {
           return mesService.AddInvoice(Input);
        }
        /// <summary>
        /// 出库列表添加
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public ApiResult<int> AddDelicery(AddDelicery Input)
        {
           return mesService.AddDelicery(Input);
        }
        /// <summary>
        /// 收款列表添加
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public ApiResult<int> AddCollection(AddCollection Input)
        {
           return mesService.AddCollection(Input);
        }
    }
}
